﻿//newUrl, rel, title, callback
window.opening = false;
function OpenTab(option) {
    if (window.opening) {
        msgwar({ message: "别催啦~已经在拼命打开页面拉~~" });
    } else {
        var $contentObj = $("#main-container");
        var url = option.url;
        var rel = option.rel;
        var title = option.title;
        var params = option.params;
        if (url) {
            if ($contentObj.data("rel") != rel || option.loadFlag) {    //首次加载
                window.opening = true;
                $contentObj.data("rel", rel);
                $contentObj.data("params", params);
                if (params) url += params;
                var loadTimer = setTimeout(function () {
                    //TODO 页面3秒内未加载则显示过渡动画
                    showload({ message: "<i class='fa fa-spin fa-refresh'></i> 正在拼命加载中，请稍后..." });
                }, 3000);

                $contentObj.load(url, function (data, status, xhr) {
                    window.opening = false;
                    clearTimeout(loadTimer);
                    hideload();
                    if (status == "success") {
                        $contentObj.data("url", url.split("?")[0]);
                        if (title) $("#main-title").html(title);
                        msgclose();
                        if (!ajaxDone(data)) {
                            //有异常则清空容器，等待下次加载
                            $(this).html("");
                        } else {
                            if (params) {
                                //TODO 页面加载查询参数
                                var tmp = params.substr(1, params.length - 1);
                                var arr = tmp.split("&");
                                for (var i = 0; i < arr.length; i++) {
                                    var arg = arr[i].split("=");
                                    $("[name='" + arg[0] + "']", $contentObj).val(unescape(arg[1]));
                                }
                            }
                            //
                            var $menuContainer = $("#sidebar-nav");
                            $(".active", $menuContainer).removeClass("active");
                            $("[rel='" + rel + "']", $menuContainer).addClass("active").parent().parent().parent().addClass("active");

                            $(document).scrollTop(0);
                            if (option.callback) option.callback();
                        }
                    } else {
                        msgerr({ message: xhr.status + " " + xhr.statusText });
                    }
                });


            } else {
                //如果容器内容为空，则刷新，否则激活
                if ($contentObj.html().length == 0) {
                    //  CloseTab(tabId);
                    $contentObj.data("rel", "");
                    OpenTab({ url: url, rel: rel, title: title });
                } else {
                    //   ActiveTab(tabId);
                    if (option.callback) option.callback();
                }
            }
        }
    }
}

function SearchTab(option) {
    var params = "";
    var $contentObj = $("#main-container");
    $("input", $('[role="SearchArea"]')).each(function (i) {
        var $obj = $(this);
        var name = $obj.attr("name");
        var val = $obj.val();
        if (name) {
            params += (params == "" ? "?" : "&") + name + "=" + escape(val);
        }
    });

    $("input", $('[role="PagerArea"]')).each(function (i) {
        var $obj = $(this);
        var name = $obj.attr("name");
        var val = $obj.val();
        if (name) {
            params += (params == "" ? "?" : "&") + name + "=" + escape(val);
        }
    });

    $("select", $('[role="PagerArea"]')).each(function (i) {
        var $obj = $(this);
        var name = $obj.attr("name");
        var val = $obj.val();
        if (name) {
            params += (params == "" ? "?" : "&") + name + "=" + escape(val);
        }
    });


    $("select", $('[role="SearchArea"]')).each(function (i) {
        var $obj = $(this);
        var name = $obj.attr("name");
        var val = $obj.val();
        if (name) {
            params += (params == "" ? "?" : "&") + name + "=" + escape(val);
        }
    });
    

    $contentObj.data("rel", "");
    OpenTab({
        url: $contentObj.data("url"),
        params: params,
        rel: $contentObj.data("rel"),
        loadFlag: true,
        callback: function () {
            if (option && option.callback) option.callback();
        }
    });


}

function ReloadTab() {
    var $contentObj = $("#main-container");
    $contentObj.load($contentObj.data("url"), function (data) {
        msgclose();
        if (!ajaxDone(data)) {
            //有异常则清空容器，等待下次加载
            $(this).html("");
        }
    });
}

function ActiveTab(tabId) {
    var contentId = tabId + "_content";
    var $tabObj = $("#" + tabId);
    var $contentObj = $("#" + contentId);
    $("#mainTab li").removeClass("active");
    $("#mainTabContent div").removeClass("active");
    $tabObj.addClass("active");
    $contentObj.addClass("active");
    $contentObj.fadeIn();
}